<?php

namespace app\http\middleware;

use think\facade\Request;
use think\facade\Session;

class SessionDeal
{
    protected $_auto_session_time;
    protected $_name ;

    public function handle($request, \Closure $next,$platform)
    {
        $token  = Request::get('token',cookie('HZEYUN_TOKEN'));
        switch ($platform){
            case "base" :
                $this->_name = 'BASE_TOKEN';
                $this->_auto_session_time = 3600;
                $token  = Request::get('token',cookie('HZEYUN_TOKEN'));
                break;
            case "admin" :
                $this->_name = 'BACK_TOKEN';
                $this->_auto_session_time = 4*3600;
                $token  = Request::get('token',cookie('BACK_TOKEN'));
                break;
            case "api" :
                $this->_name = 'API_TOKEN';
                $this->_auto_session_time = 30*24*3600;
                $token  = Request::get('token',cookie('HZEYUN_TOKEN'));
                break;
        }

//        $token  = Request::get('token',cookie('BACK_TOKEN'));
        $this->sessionSet($token);


        return $next($request);

    }


    protected function sessionSet($token=''){
        Session::init([
            'id'             => $token?$token:env('session_id'),
            // SESSION_ID的提交变量,解决flash上传跨域
            'var_session_id' => env('session_var_session_id'),
            // SESSION 前缀
            'prefix'         => env('session_prefix'),
            // 驱动方式 支持redis memcache memcached
            'type'           => env('session_type'),
            /*-------------------redis相关配置----------------------*/
            //浏览器里cookie的名字
            'name' => $this->_name,
            //redis 地址
            'host'       => env('redis_host'),
            // redis端口
            'port'       => env('redis_port'),
            // 密码
            'password'   => env('redis_password'),
            //选择存储的数据库
            'select'  => env('redis_db'),
            /*--------------------------------------------------------*/
            //sessionkey前缀
            'session_name' => env('session_session_name'),
            //session过期时间
            'expire' => $this->_auto_session_time,
            //作用域
            'domain'=>env('session_domain'),
            // 是否自动开启 SESSION
            'auto_start'     => env('session_auto_start'),
        ]);
    }
}
